package cn.jishiyu11.xeqb.utils

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

import java.lang.reflect.Type

/**
 * Created by jsy_zj on 2017/11/28.
 * 解析工具类
 */

class GsonBeanUtil<T> {

    /**
     * @author jsy_zj
     * created at 2017/11/28 17:57
     * 方法说明：复杂的数据解析
     */
    fun getListDiffictData(json: String): List<T>? {
        val g = Gson()
        val type = object : TypeToken<List<T>>() {

        }.type
        return g.fromJson<List<T>>(json, type)
    }

    /**
     * @author jsy_zj
     * created at 2017/11/28 18:16
     * 方法说明：复杂的数据解析
     */
    fun getListDiffictData2(json: String): List<T>? {
        return Gson().fromJson<List<T>>(json, object : TypeToken<List<T>>() {

        }.type)
    }

    /**
     * @author jsy_zj
     * created at 2017/11/28 18:03
     * 方法说明：单个对象解析
     */
    fun getBeanEasyData(json: String, tClass: Class<T>): T {
        var t = Any() as T
        val g = Gson()
        t = g.fromJson(json, tClass)
        return t
    }

    /**
     * @author jsy_zj
     * created at 2017/11/28 18:03
     * 方法说明：单个对象解析
     */
    fun getBeanEasyData2(json: String, tClass: Class<T>): T {
        return Gson().fromJson(json, tClass)
    }


}
